﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WZ.WebSpider
{
    public class AnnouncementSpider : IWebSpider<Entity.Announcement>
    {
        public string Code
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Url
        {
            //get { return "http://www.cninfo.com.cn/cninfo-new/disclosure/sse_latest"; }
            get { return "http://www.cninfo.com.cn/cninfo-new/announcement/query"; }
            //
        }

        public Encoding ContextEncoding
        {
            get { return Encoding.UTF8; }
        }

        public System.Text.RegularExpressions.Regex regex
        {
            get { throw new NotImplementedException(); }
        }

        public List<Entity.Announcement> GetWebData(string urlParam)
        {
            WZ.Utils.WebHttp.HttpClient client = new Utils.WebHttp.HttpClient(this.ContextEncoding);
            string postResult = client.PostString(this.Url, urlParam);
            dynamic result = Common.JsonHelper.DeserializeObject(postResult);
            string listAnn = Common.JsonHelper.SerializeObject(result.announcements);
            var q = Common.JsonHelper.DeserializeObject<List<Entity.Announcement>>(listAnn);
            List<Entity.Announcement> resultT = new List<Entity.Announcement>();
            foreach (var item in q)
            {
                //foreach (var c in item)
                //{
                    //c.Id = 0;
                    item.adjunctUrl = "http://www.cninfo.com.cn/" + item.adjunctUrl;
                    resultT.Add(item);
                //}
            }
            return resultT;
        }

        public bool Save(Entity.Announcement t)
        {
            WZ.Stock.Data.AnnouncementDAL dal = new Stock.Data.AnnouncementDAL();
            if (!dal.ExistAnnouncemnet(t.announcementId))
            {
                return dal.InsertData(t) > 0;
            }
            return false;
        }

        public int DepIndex
        {
            get { throw new NotImplementedException(); }
        }
    }
}
